<?php
namespace OrderAdmin\Controller;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\PartnerModel;
use OrderAdmin\Model\WorkerModel;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class PartnerController extends BaseController
{

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->partnerModel = new PartnerModel();
        $this->workerModel = new WorkerModel();
    }

    /**
     * 城市合伙人
     * @return [type] [description]
     */
    public function index()
    {
        $count = M('partner')->count();
        $thisMonthCount = $this->partnerModel->getThisMonthCount();

        $this->seotitle = '城市合伙人';
        $this->assign(compact('count', 'thisMonthCount'));
        $this->display();
    }



    /**
     * 分页数据
     */
    public function getData(){
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->partnerModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    /**
     * 合伙人详情页
     */
    public function detail(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('合伙人不存在');
        }
        $data = $this->partnerModel->getInfoById($id);
        if($data){
            $this->assign(compact('data'));
            $this->display();
        }else{
            $this->error('获取信息失败');
        }
    }

    /**
     * 获取合伙人下面的师傅列表
     */
    public function getWorker(){
        $param['partner_id'] = I('post.partner_id', '');
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->workerModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    /**
     * 添加修改资料页面
     */
    public function edit(){
        $type = I('get.type', '');
        if($type == 'edit'){
            $id = I('get.id', 0, 'intval');
            if($id === 0){
                $this->error('合伙人不存在');
            }
            $data = $this->partnerModel->getInfoById($id);
            $this->assign(compact('data', 'type'));
            $this->display();
        }elseif ($type == 'add'){
            $this->assign(compact('type'));
            $this->display();
        }else{
            $this->error('请求失败');
        }
    }

    /**
     * 城市合伙人添加，修改
     */
    public function update()
    {
        $type = I('post.type', '');
        if ($type == 'add') {
            $data = I('post.');
            $data['password'] = md5($data['password']);
            $data['add_time'] = time();

            $res = M('partner')->add($data);
            if ($res) {
                ajax_return(true, '创建成功');
            } else {
                ajax_return(false, '创建失败');
            }
        } elseif ($type == 'edit') {
            $data = I('post.');
            if(!$data['password']){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }
            $data['update_time'] = time();
            $res = M('partner')->where(['id' => $data['id']])->save($data);
            if ($res) {
                ajax_return(true, '编辑成功');
            } else {
                ajax_return(true, '编辑失败');
            }
        }
    }

}